package com.kaili.basic.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.entity.BaseMoveStoreEntity;
import com.kaili.basic.pojo.vo.*;
import com.kaili.basic.service.*;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import java.util.*;

@ApiVersion(1)
@RestController
@RequestMapping("/{api_version}")
@Transactional
@Api(tags = "移库管理", description = "移库管理")
public class BaseMoveStoreController {

    @Autowired
    private BaseMoveStoreService baseMoveStoreService;
    @Autowired
    private BaseStoreStockService baseStoreStockService;

    @GetMapping("/moveStore/orderNo")
    @ApiOperation(value = "查询订单号")
    public Long queryStoreOrderNo(){
        return baseMoveStoreService.queryStoreOrderNo();
    }

    @GetMapping("/moveStore")
    @ApiOperation(value = "查询移库单", notes = "查询移库单")
    public Page<BaseMoveStoreDetailVO> queryPageList(@ModelAttribute BaseMoveStoreQueryDto queryDto){
        return baseMoveStoreService.queryPageList(new Page<BaseMoveStoreDetailVO>(queryDto.getPage(),queryDto.getPageSize()),queryDto);
    }


    @GetMapping("/moveStore/edit/{id}")
    @ApiOperation(value = "编辑查询移库")
    public BaseMoveStoreDetailVO queryInStoreDetail(@PathVariable("id") Long id){
        return baseMoveStoreService.queryInStoreDetail(new HashMap<String, Object>() {{
            put("id", id);
        }});
    }

    @GetMapping("/moveStore/detail/{orderNo}")
    @ApiOperation(value = "查询移库明细")
    List<BaseMoveStoreDetailVO> queryDetailByOrderNo(@ApiParam("订单号") @PathVariable("orderNo") String orderNo){
        return baseMoveStoreService.queryDetailByOrderNo(new HashMap<String,Object>(){{put("orderNo",orderNo);}});
    }

    @GetMapping("/moveStore/export")
    @ApiOperation(value = "移库明细导出")
    public void export(HttpServletResponse response, @ModelAttribute BaseMoveStoreQueryDto queryDto) throws RestException {
        List<BaseMoveStoreExportVO> list = baseMoveStoreService.export(queryDto);
        ExcelUtil.exportExcel(list,"移库管理","sheet1", BaseMoveStoreExportVO.class, UUID.randomUUID().toString()+".xls",response);
    }

    @PostMapping("/moveStore")
    @ApiOperation(value = "新增移库单", notes = "新增移库单")
    public RestStatus addMoveStore(@RequestBody BaseMoveStoreAddDto addDto){
        return baseMoveStoreService.addMoveStore(addDto);
    }

    @PutMapping("/moveStore")
    @ApiOperation(value = "修改移库单", notes = "修改移库单")
    public RestStatus updateMoveStore(@RequestBody BaseMoveStoreEditDto addDto){
        return baseMoveStoreService.updateMoveStore(addDto);
    }

    @PutMapping("/moveStore/audit")
    @ApiOperation(value = "审核移库单", notes = "审核移库单")
    public RestStatus auditMoveStore(@RequestBody BaseMoveStoreAuditDto auditDto){
        try{
            return baseMoveStoreService.auditMoveStore(auditDto);
        }catch (Exception e){
            e.printStackTrace();
            return CommonStatus.STOCK_NOT_HAVE;
        }
    }

    @GetMapping("/moveStore/stock")
    @ApiOperation(value = "查询可出库存", notes = "查询可出库存")
    public Page<BaseStoreStockVO> queryCanOutStock(@ModelAttribute BaseStoreStockQueryDto queryDto)throws Exception{
        if(queryDto.getLocationType()!=null){
            String[] split = queryDto.getLocationType().split(",");
            List<Long> list=new ArrayList<>();
            for (int i = 0; i <split.length ; i++) {
                list.add(Long.parseLong(split[i]));
            }
            queryDto.setLocationTypes(list);
        }

        return baseStoreStockService.queryCanOutStock(new Page<BaseStoreStockVO>(queryDto.getPage(),queryDto.getPageSize()),queryDto);
    }

    @DeleteMapping("/moveStore/{id}")
    @ApiOperation(value = "删除移库")
    public RestStatus deleteInStore(@ApiParam("订单号id")@PathVariable("id") long id){
        BaseMoveStoreEntity entity=new BaseMoveStoreEntity();
        entity.setId(id);
        entity.setStatus("0");
        baseMoveStoreService.updateById(entity);
        return CommonStatus.DELETE_OK;
    }
}


